import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class CopyDem07 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件绝对路径");
        String path = scanner.next();//C:\Users\Administrator\Pictures\test.jpg
        FileInputStream fileInputStream = new FileInputStream(path);// 输入流


        // 处理文件的名字
        String[] paths = path.split("\\\\");//[c:,Users,Administrator,Pictures,test.test.jpg]
        String file_name = paths[paths.length - 1];// test.test.jpg
        System.out.println(file_name);
        String[] names = file_name.split("\\.");//[test,test,jpg]
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < names.length; i++) {
            sb.append(names[i]);//test.test_bak.jpg
            if (i != names.length - 2 && i != names.length - 1) {
                sb.append(".");
            }
            if (i == names.length - 2) {
                sb.append("_bak.");
            }
        }
        // test_bak.jpg
        // 拼上路径
        StringBuilder copy_file_path = new StringBuilder();
        for (int i = 0; i < paths.length; i++) {
            if (i != paths.length - 1) {
                copy_file_path.append(paths[i]);
                copy_file_path.append(File.separator);//   c:/Users/Adminstrator/Pictures/
            } else {
                copy_file_path.append(sb);//  c:/Users/Adminstrator/Pictures/test_bak.jpg
            }
        }
        FileOutputStream outputStream = new FileOutputStream(copy_file_path.toString());// 输出流
        byte[] bytes = new byte[1024];
        int len;
        // 复制的逻辑
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);// 读多少，写多少
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }

    }
}
